package com.ece.pfe_rover_pvt_ines_ece_application.view;

import com.ece.pfe_rover_pvt_ines_ece_application.PFE_ROVER_PVT_INES_ECE_Application;
import com.ece.pfe_rover_pvt_ines_ece_application.R;
import com.parse.ParseAnonymousUtils;
import com.parse.ParseObject;
import com.parse.ParseUser;
import com.parse.ui.ParseLoginActivity;
import com.parse.ui.ParseLoginBuilder;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {
	private static final int LOGIN_ACTIVITY_CODE = 100;
	public boolean DEBUG_MODE = PFE_ROVER_PVT_INES_ECE_Application.DEBUG;
	
	private Button admButton,
				   usrButton,
				   authentButton;
	private TextView loggedInInfoView;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		admButton = (Button) findViewById(R.id.btAdmin);
		usrButton = (Button) findViewById(R.id.btUser);
		authentButton = (Button) findViewById(R.id.btGraphs);
		loggedInInfoView = (TextView) findViewById(R.id.loggedin_info);
							
		admButton.setVisibility(View.INVISIBLE);
		usrButton.setVisibility(View.INVISIBLE);
		
		authentButton.setOnClickListener(new OnClickListener() {		
			@Override
			public void onClick(View v) {
				Intent i = new Intent(getBaseContext(), ParseLoginActivity.class);		
				startActivityForResult(i, LOGIN_ACTIVITY_CODE);
				if((ParseUser.getCurrentUser() != null)) {
					if(ParseUser.getCurrentUser().getObjectId().equals(PFE_ROVER_PVT_INES_ECE_Application.ADMIN_ID)) {					
					admButton.setVisibility(View.VISIBLE);
				}else if(!ParseAnonymousUtils.isLinked(ParseUser.getCurrentUser())) {
					usrButton.setVisibility(View.VISIBLE);
				}			}					
			}
		});
		
		admButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// ask for authentication
				// redirect toward Adm dashboard
				
				//Bug au logout ici :
				Intent i = new Intent(getBaseContext(), ParseLoginActivity.class);
				
				startActivityForResult(i, LOGIN_ACTIVITY_CODE);
				if(ParseUser.getCurrentUser().getObjectId().equals(PFE_ROVER_PVT_INES_ECE_Application.ADMIN_ID)) {
					Intent admIntent = new Intent(getBaseContext(), AdmDashboardActivity.class);
					startActivity(admIntent);
				}				
			}
		});
	}

	protected void onResume() {			
		super.onResume();
		// Check if we have a real user
		
		if (ParseUser.getCurrentUser() != null && (!ParseAnonymousUtils.isLinked(ParseUser.getCurrentUser()))) {			
			// Update the logged in label info
			updateLoggedInInfo();
			updateActivatedButton();
		}	
	}
	
	/**
	 * Utility function to show or hide button
	 * according to the ACL of the user
	 */
	private void updateActivatedButton() {
		if(ParseUser.getCurrentUser() != null &&(ParseUser.getCurrentUser().getObjectId().equals(PFE_ROVER_PVT_INES_ECE_Application.ADMIN_ID))) {					
			admButton.setVisibility(View.VISIBLE);
			authentButton.setVisibility(View.INVISIBLE);
		}else if(!ParseAnonymousUtils.isLinked(ParseUser.getCurrentUser())) {
			usrButton.setVisibility(View.VISIBLE);
			authentButton.setVisibility(View.INVISIBLE);
		}
		
		if(ParseAnonymousUtils.isLinked(ParseUser.getCurrentUser())) 
				authentButton.setVisibility(View.VISIBLE);
	}
	
	/**
	 * Notification to show who has logged in
	 */
	private void updateLoggedInInfo() {
		
		if(DEBUG_MODE) System.out.println("---- Entering updateLoggedInfo method...");
		
		if ((ParseUser.getCurrentUser() != null) && !ParseAnonymousUtils.isLinked(ParseUser.getCurrentUser())) {
			ParseUser currentUser = ParseUser.getCurrentUser();
			loggedInInfoView.setText(getString(R.string.logged_in,
					currentUser.getString("name")));
		} else {
			loggedInInfoView.setText(getString(R.string.not_logged_in));
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.			
		getMenuInflater().inflate(R.menu.action_login, menu);
		getMenuInflater().inflate(R.menu.action_logout, menu);									
		
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		// Handle action bar item clicks here. The action bar will 
		int id = item.getItemId();
		switch(id) {				
		case R.id.action_logout:		
			// Log out the current user
			ParseUser.logOut();
			// Create a new anonymous user
			ParseAnonymousUtils.logIn(null);
			// Update the logged in label info
			updateLoggedInInfo();
			authentButton.setVisibility(View.VISIBLE);
			admButton.setVisibility(View.INVISIBLE);
			usrButton.setVisibility(View.INVISIBLE);
			// Clear the view
			//MeasurementsListAdapter.clear();
			// Unpin all the current objects
			ParseObject
					.unpinAllInBackground(PFE_ROVER_PVT_INES_ECE_Application.MEASUREMENTS_GROUP_NAME);			
			return super.onOptionsItemSelected(item);
			
		case R.id.action_login:
			invalidateOptionsMenu();
			ParseLoginBuilder builder = new ParseLoginBuilder(this);
			startActivityForResult(builder.build(), LOGIN_ACTIVITY_CODE);
			
			return super.onOptionsItemSelected(item);
		default:			
				return super.onOptionsItemSelected(item);
		}
	}
	
	/**
	 * onPrepareOptionsMenu
	 */
	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {		
		
		if(DEBUG_MODE) System.out.println("---- Entering onPrepareOptopnsMenu method...");
		
		boolean realUser;
		super.onPrepareOptionsMenu(menu);
		if(ParseUser.getCurrentUser() != null){
			realUser = !ParseAnonymousUtils.isLinked(ParseUser
				.getCurrentUser());		
			menu.findItem(R.id.action_login).setVisible(!realUser);						
			menu.findItem(R.id.action_logout).setVisible(realUser);				
		}					
		return true;
	}
}
